local nDltMapping ={{-1, -1, 6}, --0+2
					{-1, -1, 6}, --1+2
					{-1, 6, 5}, --2+1，2+2
					{6, 5, 4}, --3+0，3+1，3+2
					{5, 4, 3}, --4+0，4+1，4+2
					{3, 2, 1}}; --5+0，5+1，5+2

require"common"
--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
        if len % 2 ~= 0 then
                return nil
        end

        for i=1,len,2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) <1 then return nil end
                if not in_table( c, st ) then
                        table.insert( st, c )
                else
                        return nil
                end
        end

        return true
end

function in_table ( e, t )
        for _,v in pairs(t) do
                if (v==e) then return true end
        end
        return false
end


--====================复式计算=============================================
function DltCompute(nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0,0,0}

	if nTotalRed < 5 or nTotalBlue < 2 or nTotalRed < nMaxBingoRed or nTotalBlue < nMaxBingoBule or nDltMapping[nMaxBingoRed+1][nMaxBingoBule+1] == -1 then
		return nBingo
	end

	for i=0,nMaxBingoRed do
		for j=0,nMaxBingoBule do
			if	nDltMapping[i+1][j+1] ~= -1  then
				if	j == 2 then
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i)
				elseif j == 1 then
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 1) * nMaxBingoBule
				else
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 2)
				end
			end
		end
	end
	return nBingo--[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]


end
--======================胆拖计算===============================================
function DltComputeDT(nTotalRedDT, nBingoRedDT, nTotalBlueDT, nBingoBuleDT,nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0,0,0}
	local donothing = 0
	if nTotalRedDT + nTotalRed < 5 or nTotalBlueDT + nTotalBlue < 2 or nTotalRedDT > 4 or nTotalBlueDT > 1 or nTotalRed < nMaxBingoRed
	or nTotalBlue < nMaxBingoBule or nTotalRedDT < nBingoRedDT or nTotalBlueDT < nBingoBuleDT then
		return nBingo
	end
	for i=0,nMaxBingoRed do
		for j=0,nMaxBingoBule do
			if i + nTotalRedDT > 5 or j + nTotalBlueDT > 2 or nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1] == -1 then
				donothing = donothing + 1
			else
				if j == 2 then
					nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i)
				elseif j == 1 then
					if nTotalBlueDT == 0 then
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * (nTotalBlue - nMaxBingoBule) * nMaxBingoBule
					else
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * nMaxBingoBule
					end
				else
					if nTotalBlueDT == 0 then
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 2)
					elseif nTotalBlueDT == 1 then
						nBingo[nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1]] = nBingo[nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * (nTotalBlue - nMaxBingoBule)
					else
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i)

					end
				end

			end
		end
	end

	return nBingo--[1]--..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]
end

--======================================================================================================================================================================================
--返回注数,倍投数 , 投注串, 指令串
function random_dlt( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	local tempStr = ""
	tempStr = string.gsub(inStr,"#Z","")
	itemList = Split( tempStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( tempStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  2#注数#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local qnums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) --前区吗
			local hnums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12) --后区吗
			zlStr = zlStr..format_num(qnums[1])..format_num(qnums[2])..format_num(qnums[3])..format_num(qnums[4])..format_num(qnums[5])..format_num(hnums[1])..format_num(hnums[2])

			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		if string.find(inStr,"#Z") then
			zlStr = zlStr.."#Z"
		end
		butNum,butPower,butStr = check_dlt('1', zlStr)
	end

	return butNum, butPower, butStr, zlStr


end

--返回注数,倍投数,投注串
function check_dlt( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	local tempStr = ""
	tempStr = string.gsub(inStr,"#Z","")
	itemList = Split( tempStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( tempStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower > 99 or butPower < 1 then return 0 end

	--单式自选 21#注码#B倍数  21#05081314320811#06081314320911
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 14 then return 0 end
			qqNum = string.sub(xItem[i],1,10)  --前区号码
			hqNum = string.sub(xItem[i],11,14)  --后区号码
			if not judge_str(qqNum,35) or not judge_str(hqNum,12) then return 0 end
		end
	--复式自选 23#前区#后区 23#01020304050607#0308
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if string.len(xItem[1]) > 36 then return 0 end
		if #xItem ~= 2 then return 0 end
		butNum = combination(string.len(xItem[1])/2,5)*combination(string.len(xItem[2])/2,2)
		if not judge_str(xItem[1],35) or not judge_str(xItem[2],12) then return 0 end



	elseif butType == '3' then
		xItem = Split(itemList[1], "#")
		--胆拖 24#前区胆码#前区拖码#后区胆码#后区拖码
		if #xItem == 4 then
			if string.len(xItem[1])/2 > 4 or (string.len(xItem[2])/2 + string.len(xItem[1])/2) < 5 or  string.len(xItem[3])/2 ~= 1 or (string.len(xItem[3])/2 + string.len(xItem[4])/2) < 3 then return 0 end
			if not judge_str(xItem[1],35) or not judge_str(xItem[2],35) or not judge_str(xItem[3],12) or not judge_str(xItem[4],12)  then return 0 end
			if  is_str_mix(xItem[1],xItem[2]) or  is_str_mix(xItem[3],xItem[4]) then return 0 end
			butNum = combination(string.len(xItem[2])/2,(5-string.len(xItem[1])/2))*combination(string.len(xItem[4])/2,(2-string.len(xItem[3])/2))
		elseif #xItem == 3 then
			--或 24#前区单式或复式注码#后区胆码#后区拖码
			if string.len(xItem[1])/2 > 4 then
				if (string.len(xItem[2])/2 + string.len(xItem[3])/2) < 3 or string.len(xItem[2])/2 ~= 1 then return 0 end
				if  is_str_mix(xItem[2],xItem[3]) then return 0 end
				if not judge_str(xItem[1],35) or not judge_str(xItem[2],12) or not judge_str(xItem[3],12) then return 0 end
				butNum = combination(string.len(xItem[1])/2,5)*combination(string.len(xItem[3])/2,1)
			--或 24#前区胆码#前区拖码#后区单式或复式注码
			else
				if string.len(xItem[1])/2 > 4 or (string.len(xItem[2])/2 + string.len(xItem[1])/2) < 6 or string.len(xItem[3])/2 < 2 then return 0 end
				if  is_str_mix(xItem[1],xItem[2]) then return 0 end
				if not judge_str(xItem[1],35) or not judge_str(xItem[2],35) or not judge_str(xItem[3],12) then return 0 end
				butNum = combination(string.len(xItem[2])/2,(5-string.len(xItem[1])/2))*combination(string.len(xItem[3])/2,2)
			end
		end
	else
		return 0
	end;
	return butNum,butPower,inStr,inStr
end
--=====================================中奖查询==================================================
function query_dlt(butType,queryStr,djStr)
    local nBingo = {0,0,0,0,0,0,0,0}
	local temp = {0,0,0,0,0,0,0,0}
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	local bingoNum = {}
	local qhStr = {}
	local nTotalRed = 0
	local nTotalBlue = 0
	local nTotalRedDT = 0
	local nTotalBlueDT = 0
	local nBingoRedDT = 0
	local nBingoBuleDT = 0

	queryStr = string.gsub(queryStr,"#Z","")
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	bullStr = Split(djStr, "|")
	if butType == '1' then
		jItem = Split(itemList[1], "#")
		nTotalRed = 5
		nTotalBlue = 2
		for i = 1,table.getn(jItem) do
			qhStr[1] = string.sub(jItem[i],1,10)
			qhStr[2]= string.sub(jItem[i],11,14)

			for i = 1,2 do
				bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
			end
			temp = nBingo
			nBingo = DltCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
			for j = 1, 8 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end

	elseif butType == '2' then
		jItem = Split(itemList[1], "#")
		nTotalRed = string.len(jItem[1]) / 2
		nTotalBlue = string.len(jItem[2]) / 2
		for i = 1,2 do
			bingoNum[i] = ConputeBingoNum(jItem[i],bullStr[i],2)
		end
		nBingo = DltCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
	elseif butType == '3' then
		jItem = Split(itemList[1], "#")
		--胆拖 24#前区胆码#前区拖码#后区胆码#后区拖码
		if #jItem == 4 then
			nTotalRedDT = string.len(jItem[1]) / 2   --红球胆码
			nTotalBlueDT = string.len(jItem[3]) / 2	--篮球胆码
			nTotalRed = string.len(jItem[2]) / 2   --红球拖码
			nTotalBlue = string.len(jItem[4]) / 2	--篮球拖码
			bingoNum[1] = ConputeBingoNum(jItem[1],bullStr[1],2)    --红球胆码
			bingoNum[2] = ConputeBingoNum(jItem[2],bullStr[1],2) 	--红球拖码
			bingoNum[3] = ConputeBingoNum(jItem[3],bullStr[2],2)	--篮球胆码
			bingoNum[4] = ConputeBingoNum(jItem[4],bullStr[2],2)	--篮球拖码

		elseif #jItem == 3 then
			--或 24#前区单式或复式注码#后区胆码#后区拖码
			if string.len(jItem[1])/2 > 4 then
				nTotalRedDT = 0  --红球胆码
				nTotalBlueDT = string.len(jItem[2]) / 2	--篮球胆码
				nTotalRed = string.len(jItem[1]) / 2   --红球拖码
				nTotalBlue = string.len(jItem[3]) / 2	--篮球拖码
				bingoNum[1] = 0    --红球胆码
				bingoNum[2] = ConputeBingoNum(jItem[1],bullStr[1],2) 	--红球拖码
				bingoNum[3] = ConputeBingoNum(jItem[2],bullStr[2],2)	--篮球胆码
				bingoNum[4] = ConputeBingoNum(jItem[3],bullStr[2],2)	--篮球拖码
			--或 24#前区胆码#前区拖码#后区单式或复式注码
			else
				nTotalRedDT = string.len(jItem[1]) / 2   --红球胆码
				nTotalBlueDT = 0--篮球胆码
				nTotalRed = string.len(jItem[2]) / 2   --红球拖码
				nTotalBlue = string.len(jItem[3]) / 2	--篮球拖码
				bingoNum[1] = ConputeBingoNum(jItem[1],bullStr[1],2)    --红球胆码
				bingoNum[2] = ConputeBingoNum(jItem[2],bullStr[1],2) 	--红球拖码
				bingoNum[3] = 0	--篮球胆码
				bingoNum[4] = ConputeBingoNum(jItem[3],bullStr[2],2)	--篮球拖码
			end
		end
		nBingo = DltComputeDT(nTotalRedDT,bingoNum[1],nTotalBlueDT,bingoNum[3],nTotalRed,bingoNum[2],nTotalBlue,bingoNum[4])
	end
	for i=1 ,8 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]
end

---print(random_dlt('1',"2#Z#B3"))

--print(check_dlt('1',"10081314321012#06081314320911#B10#Z"))
--print(check_dlt('2',"0102030405#0308"))
--print(check_dlt('3',"0608#0911121416#03#081112#B1#Z"))
--print(check_dlt('3',"100813#060912#01#1112#Z#B10"))
--print(check_dlt('3',"01#2425282931#04060810#B1"))
--print(check_dlt('3',"100813#010203#1112#B10#Z"))
--print(query_dlt('2',"07131927303133#0409#B1","0713273034|0112"))
--print(query_dlt('1',"01020304050607#B1","0102030405|0607"))  --DltComputeDT(2, 2, 0, 0, 5, 3, 3, 1)
--print(DltComputeDT(2, 2, 1, 1, 5, 3, 3, 1))
